package ControlLayer;

import java.util.ArrayList;

import DBLayer.*;
import ModelLayer.*;

public class CreateControler implements IFCreate {
	private DbContact contactDB;
	private DbEmployee employeeDB;
	private DbProduct productDB;
	private DbSalesOrder salesOrderDB;
	private DbDelivery deliveryDB;
	
	public CreateControler()
	{
		contactDB = new DbContact();
		employeeDB = new DbEmployee();
		productDB = new DbProduct();
		salesOrderDB = new DbSalesOrder(); 
		deliveryDB = new DbDelivery();
	}

	@Override
	public int getNextContactId() {
		return contactDB.getNextContactId()+1;
	}

	@Override
	public int getNextProductId() {
		return productDB.getNextProductId()+1;
	}

	@Override
	public int getNextDeliveryId() {
		return deliveryDB.getNextDeliveryId()+1;
	}
	
	@Override
	public int getNextSalesOrderId() {
		return salesOrderDB.getNextSalesOrderId()+1;
	}

	@Override
	public int createCustomer(Contact customer) throws Exception {
		return contactDB.insertCustomer(customer);
	}

	@Override
	public int createOrder(SalesOrder salesOrder) throws Exception{
		return salesOrderDB.insertSalesOrder(salesOrder);
	}

	@Override
	public int createDelivery(Delivery delivery) throws Exception {
		return  deliveryDB.insertDelivery(delivery);
	}

	@Override
	public int createEmployee(Employee employee) throws Exception {
		return  employeeDB.insertEmployee(employee);	
	}

	@Override
	public int createProduct(Product product) throws Exception {
		return productDB.insertProduct(product);
	}

	@Override
	public double calculateTotal(ArrayList<SalesLine> salesLine) {
		double result = 0.0;
		for(int i=0;i<salesLine.size();i++)
		{
			result = result + (salesLine.get(i).getQuantity() * (salesLine.get(i).getP().getNettPrice() + 
					((salesLine.get(i).getP().getTax() * salesLine.get(i).getP().getNettPrice()) / 100)));
		}
		return result;
	}
}
